package artid.com.arttogo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.Type;
import com.hyphenate.chat.EMTextMessageBody;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.view.CircleImageView;

/**
 * 
 *    
 * 项目名称：ArtworkArt   
 * 类名称   ：PersonMessageAdapter
 * 类描述   ：个人消息适配器  
 * 创建人   ：耿卫斌   
 *
 */
public class PersonMessageAdapter extends BaseAdapter {
	private Context mContext;
	private List<EMConversation> list;

	public PersonMessageAdapter(Context mContext, List<EMConversation> list) {
		this.mContext = mContext;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_personmessage, null);
			holder.icon=(CircleImageView) v.findViewById(R.id.item_personmessage_icon);
			holder.unreadMessage_icon=(ImageView) v.findViewById(R.id.item_personmessage_unreadMessage);
			holder.name=(TextView) v.findViewById(R.id.item_personmessage_Name);
			holder.time=(TextView) v.findViewById(R.id.item_personmessage_time);
			holder.content=(TextView) v.findViewById(R.id.item_personmessage_content);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		String friendAvatar = "";//好友头像
		String fromName="";//好友ID（也是名字）
		holder.time.setText(formatUnixTime(list.get(position).getLastMessage().getMsgTime()));
		Type type = list.get(position).getLastMessage().getType();
		if(type== EMMessage.Type.TXT){
			EMTextMessageBody txtBody = (EMTextMessageBody) list.get(position).getLastMessage().getBody();
			holder.content.setText(txtBody.getMessage());
		}else{
			holder.content.setText("/图片/");
		}
		friendAvatar=list.get(position).getLastMessage().getStringAttribute("A_head","http://172.16.0.3/upload/artPlatform/Addons/Avatar/default_128_128.png");
		holder.icon.setTag(friendAvatar);
		ImageLoader.getInstance().displayImage(friendAvatar, holder.icon);
		fromName=list.get(position).getLastMessage().getStringAttribute("A_name",list.get(position).conversationId());
		holder.name.setText(fromName);
		EMConversation conversation = EMClient.getInstance().chatManager().getConversation(list.get(position).conversationId());
		if(conversation.getUnreadMsgCount()!=0){
			holder.unreadMessage_icon.setVisibility(View.VISIBLE);
		}else{
			holder.unreadMessage_icon.setVisibility(View.GONE);
		}
		return v;
	}
	class ViewHolder {
		CircleImageView icon;
		ImageView unreadMessage_icon;
		TextView name,time,content;
	}

	// 将时间戳转为字符串
	public String formatUnixTime(Long time) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
		Date date=new Date(time);
		String re_StrTime = dateFormat.format(date);
		return re_StrTime;	
	}

}
